package com.shortlink.admin.dao.entity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.TableName;
import com.shortlink.common.base.BaseDO;
import com.shortlink.common.user.UserInfoDTO;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@AllArgsConstructor
@TableName("t_domain")
public class DomainDO extends BaseDO {
    public static final Integer USER_GROUP_AUTH = 1;
    public static final Integer USER_AUTH = 2;
    public static final Integer NUMBER_RULE = 1;
    /**
     * 主键ID
     */
    private Long id;
    /**
     * 权限类别 0-无权限 1-用户组权限 2-用户权限
     */
    private Integer authType;
    /**
     * 用户ID
     */
    private Long userId;
    /**
     * 用户组级别
     */
    private Long userGroupLevel;
    /**
     * 域名
     */
    private String domain;
    /**
     * 短链接生成算法 0-HASH法 1-发号器法
     */
    private Integer shortUriRule;
    /**
     * 删除标识 0：未删除 1：已删除
     */
    private Integer delFlag;

    /**
     * 校验域名用户权限
     */
    public boolean verifyDomainAuth(UserInfoDTO userInfoDTO) {
        if (USER_GROUP_AUTH.equals(authType)) {
            Integer currentUserGroupLevel = userInfoDTO.getGroupLevel();
            if (ObjectUtil.isEmpty(userGroupLevel)) {
                return true;
            } else if (ObjectUtil.isEmpty(currentUserGroupLevel)) {
                return false;
            } else {
                return currentUserGroupLevel >= userGroupLevel;
            }
        } else if (USER_AUTH.equals(authType)) {
            return ObjectUtil.isEmpty(userId) || userId.equals(userInfoDTO.getUserId());
        } else {
            return true;
        }
    }
}